package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopUser;
import com.bdqn.pojo.ShopUserCountRateVO;
import com.bdqn.pojo.ShopUserVO;
import com.bdqn.service.ShopUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户
 *
 * @author lilibo
 * @date 2021/10/09
 * @date 2021/10/05
 */
@Api(tags="用户接口")
@RestController
@RequestMapping("/shopuser")
public class ShopUserController {

    @Autowired
    private ShopUserService shopUserService;

    /**
     * 新增用户记录
     *
     * @param shopUser
     * @return R
     */
    @ApiOperation("新增用户记录")
    @WebLogger("新增用户记录")
    @PostMapping("/add")
    public R save(@ApiParam("用户对象") @RequestBody ShopUser shopUser) {
        return new R<>(shopUserService.save(shopUser));
    }

    /**
     * 修改用户记录
     *
     * @param shopUser
     * @return R
     */
    @ApiOperation("修改用户记录")
    @WebLogger("修改用户记录")
    @PostMapping("/update")
    public R update(@ApiParam("用户对象") @RequestBody ShopUser shopUser) {
        return new R<>(shopUserService.updateById(shopUser));
    }

    /**
     * 通过id删除一条用户记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条用户记录")
    @WebLogger("通过id删除一条用户记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("用户id") Integer id) {
        return new R<>(shopUserService.removeById(id));
    }

    /**
     * 通过id查询单条用户记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条用户记录")
    @WebLogger("通过id查询单条用户记录")
    @GetMapping("/{id}")
    public R<ShopUser> getById(@ApiParam("用户id") @PathVariable Integer id) {
        return new R<>(shopUserService.getById(id));
    }

    /**
     * 通过用户信息查询用户记录
     *
     * @param shopUser 用户信息对象
     * @return R
     */
    @ApiOperation("通过用户信息查询用户记录")
    @WebLogger("通过用户信息查询用户记录")
    @GetMapping("/list")
    public R<List<ShopUser>> getList(@ApiParam("查询的用户对象") ShopUser shopUser) {
        return new R<>(shopUserService.getList(shopUser));
    }

    /**
     * 分页查询用户
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopUser 用户
     * @return
     */
    @ApiOperation("分页查询用户")
    @WebLogger("分页查询用户")
    @GetMapping("/page")
    public R<IPage<ShopUser>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的用户对象") ShopUser shopUser) {
        return new R<>(shopUserService.getPage(new Page<>(pageNo, pageSize), shopUser));
    }

    /**
     * 分页查询用户数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopUser 用户
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询用户数据")
    @WebLogger("分页查询用户数据")
    @GetMapping("/pagelist")
    public R<List<ShopUser>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的用户对象") ShopUser shopUser) {
        IPage<ShopUser> page = shopUserService.getPage(new Page<>(pageNo, pageSize), shopUser);
        return new R<>(page.getRecords(), page.getTotal());
    }

    /**
     * 用户数统计
     *
     * @return
     */
    @ApiOperation("用户数统计")
    @WebLogger("用户数统计")
    @GetMapping("/countRate")
    public R<ShopUserCountRateVO> getCountRate() {
        return new R<>(shopUserService.getCountRate());
    }

    /**
     * 用户数统计
     *
     * @return
     */
    @ApiOperation("用户和账户")
    @WebLogger("用户和账户")
    @GetMapping("/List")
    public R<List<ShopUserVO>> getCoun() {
        return new R<>(shopUserService.getAll());
    }



}
